package j06zuoye.studentmanage;


import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

public class StudentDao {
    //创建学生数组
    Student[] students = new Student[10];
    //导包
    Random r = new Random();
    Scanner sc = new Scanner(System.in);
    //id  getId  getRandomId
    String Id() {
        String str = "";
        int[] arr = {0,1,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122};
        for (int i = 0; i < 5; i++) {
            int serial = r.nextInt(54);
            if (serial < 2){
                str += arr[serial];
            } else {
                char letter = (char) arr[serial];
                str += letter;
            }
        }
        return str;
    }
    //页面
    public  void ui(){
        System.out.println("*******************");
        System.out.println("学生信息管理系统");
        System.out.println("*******************");
        System.out.println("1 - 添加学生信息");
        System.out.println("2 - 修改学生信息");
        System.out.println("3 - 删除学生信息");
        System.out.println("4 - 根据编号查询学生信息");
        System.out.println("5 - 根据姓名模糊查询学生信息");
        System.out.println("6 - 根据年龄范围查询学生信息");
        System.out.println("7 - 查询所有学生信息");
        System.out.println("8 - 退出系统");
        System.out.println("*******************");
        Value();
    }
    //传值
    void Value(){
        String id;
        while (true){
            System.out.print("请输入你的选择:");
            //menu
            int serial = sc.nextInt();
            sc.nextLine();
            switch (serial){
                //添加
                case 1:
                    addPrint();
                    break;
                //修改
                case 2:
                    updatePrint();
                    break;
                //删除
                case 3:
                    deletePrint();
                    break;
                //编号查询
                case 4:
                    System.out.print("请输入要查询的学生编号：");
                    id = sc.nextLine();
                    selectById(id);
                    break;
                //姓名查询
                case 5:
                    selectByName();
                    break;
                //年龄范围查询
                case 6:
                    selectByAge();
                    break;
                //查询
                case 7:
                    selectAll();
                    break;
                //退出
                case 8:
                    System.out.println("确定退出系统吗(Y/N)？");
                    switch (sc.nextLine()){
                        case "Y":
                            System.out.println("程序退出中...");
                            return;
                        case "N":
                            System.out.println("退出失败！");
                            break;
                    }
                    break;
                default:
                    System.out.println("输入格式不正确!");
                    System.out.println("*******************");
                    break;
            }
        }
    }
    Student addFour(int num){
        return new Student(
                Id(),
                sc.nextLine(),
                sc.nextLine(),
                sc.nextInt()
        );
    }
    //添加
    void addPrint(){
        System.out.println("请输入要添加的学生信息：");
        int num = 0;
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null){
                students[i] = new Student(
                        Id(),
                        sc.nextLine(),//name
                        sc.nextLine(),//sex
                        sc.nextInt()//age
                );
                num = i;
                break;
            }
        }
        if (students[num] != null){
            System.out.println("添加成功！");
            System.out.println("students[" + num + "] = " + students[num]);
        }
    }
    //插入
    void insert(){
        System.out.print("请输入要插入的位置：");
        int num = sc.nextInt();
        Student student = addFour(num);
        if (students[num] != null) {
            int i = num + 1;
            int max = i;
            while (students[i] != null) {
                i++;
                max = i;
            }
            for (int j = max; j >= num; j--) {
                Student stu = students[j];
                students[j] = students[j - 1];
                students[j - 1] = stu;
            }
        }
        students[num] = student;
        System.out.println("插入成功！");
    }
    //修改
    void update(String id){
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null){
                continue;
            }
            if (Objects.equals(id, students[i].id)){
                students[i].name = sc.nextLine();
                students[i].sex = sc.nextLine();
                students[i].age = sc.nextInt();
            }
        }
    }
    void updatePrint(){
        System.out.print("请输入要修改的学生编号：");
        String id = sc.nextLine();
        boolean isSame = false;
        int num = 0;
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null){
                continue;
            }
            if (Objects.equals(students[i].id, id)){
                System.out.println("students[" + i + "] = " + students[i]);
                System.out.println("请输入要修改的学生信息：");
                update(id);
                isSame = true;
                num = i;
                break;
            }
        }
        if (!isSame){
            System.out.println("没有编号为" + id + "的学生！");
        }
        System.out.println("修改成功！");
        System.out.println("students[" + num + "] = " + students[num]);
    }
    //3.删除
    void deletePrint(){
        System.out.print("请输入要删除的学生id：");
        String id = sc.nextLine();
        boolean isSame = false;
        int num = 0;
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null){
                continue;
            }
            if (Objects.equals(students[i].id, id)){
                num = i;
                System.out.println("students[" + i + "] = " + students[i]);
                delete(i);
                isSame = true;
            }
        }
        if (!isSame){
            System.out.println("没有编号为" + id + "的学生！");
        }else {
            for (int i = num; i < students.length-1; i++) {
                if (students[i+1] != null){
                    students[i] = students[i+1];
                }else {
                    students[i] = students[i+1];
                    return;
                }
            }
        }
    }
    //是否删除
    void delete(int i){
        System.out.println("确定删除吗(Y/N)？");
        String isDelete = sc.nextLine();
        switch (isDelete){
            case "Y":
                students[i] = null;
                System.out.println("删除成功！");
                break;
            case "N":
                System.out.println("删除失败！");
                break;
        }
    }
    //4.Id查找
    void selectById(String id){
        boolean isSelect = false;
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null){
                continue;
            }
            if (Objects.equals(students[i].id, id)){
                System.out.println("students[" + i + "] = " + students[i]);
                isSelect = true;
            }
        }
        if (!isSelect){
            System.out.println("没有查询到编号为" + id + "的学生！");
        }
    }
    //5.关键字查询
    void selectByName(){
        System.out.print("请输入要查询的关键字：");
        String keyName = sc.nextLine();
        boolean isName = false;
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null){
                continue;
            }
            if (students[i].name.contains(keyName)){
                System.out.println("students[" + i + "] = " + students[i]);
                isName = true;
            }
        }
        if (!isName){
            System.out.println("没有姓名中包含" + keyName + "的学生！");
        }
    }
    //6.年龄范围查找
    void selectByAge(){
        System.out.print("请输入要查询的最小年龄和最大年龄：");
        int ageMin = sc.nextInt();
        int ageMax = sc.nextInt();sc.nextLine();
        boolean isAge = false;
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null){
                continue;
            }
            if (ageMin>ageMax){
                System.out.println("年龄输入错误！");
                return;
            } else if (students[i].age >= ageMin && students[i].age <= ageMax) {
                System.out.println("students[" + i + "] = " + students[i]);
                isAge = true;
            }
        }
        if (!isAge){
            System.out.println("没有查询到年龄在" + ageMin + "到" + ageMax + "之间的学生！");
        }
        System.out.println("*******************");
    }
    //7.全局查找
    void  selectAll(){
        for (int i = 0; i < students.length; i++) {
            System.out.println("students[" + i + "] = " + students[i]);
        }
        System.out.println("*******************");
    }
}
